package com.qysoft.modules.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * @author liugong
 */
public class IOUtil {

	public static byte[] readInputStream(InputStream inputStream) throws Exception{ 
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(bytes)) != -1) {
			byteArrayOutputStream.write(bytes, 0, len);
		}
		inputStream.close();
		return byteArrayOutputStream.toByteArray();
	}
	
	public static byte[] readFile(String filename) throws Exception{ 
		FileInputStream fileInputStream = new FileInputStream(new File(filename));
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[1024];
		int len = 0;
		while ((len = fileInputStream.read(bytes)) != -1) {
			byteArrayOutputStream.write(bytes, 0, len);
		}
		fileInputStream.close();
		return byteArrayOutputStream.toByteArray();
	}
	
	public static void wirteFile(byte[] data,String filename) throws Exception{
		FileOutputStream fileOutputStream = new FileOutputStream(new File(filename));
		fileOutputStream.write(data);
		fileOutputStream.flush();
		fileOutputStream.close();
	}
}
